<?php
//判断用户是否登录
if(!isset($_COOKIE['username']) || !isset($_COOKIE['qx'])){
	header('location:../login/login.php');
	exit;
}
//包含数据库配置文件
include '../../public/dbconfig.php';
//包含函数库文件
include '../../public/functions.php';
//设置字符集
header('Content-type:text/html;charset=utf-8');
//调用链接数据库函数
$link = con();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主要内容区main</title>
<link href="../css/css.css" type="text/css" rel="stylesheet" />
<link href="../css/main.css" type="text/css" rel="stylesheet" />
<link rel="shortcut icon" href="../images/main/favicon.ico" />
<style>
body{overflow-x:hidden; background:#f2f0f5; padding:15px 0px 10px 5px;}
#searchmain{ font-size:12px;}
#search{ font-size:12px; background:#548fc9; margin:10px 10px 0 0; display:inline; width:100%; color:#FFF; float:left}
#search form span{height:40px; line-height:40px; padding:0 0px 0 10px; float:left;}
#search form input.text-word{height:24px; line-height:24px; width:180px; margin:8px 0 6px 0; padding:0 0px 0 10px; float:left; border:1px solid #FFF;}
#search form input.text-but{height:24px; line-height:24px; width:55px; background:url(../images/main/list_input.jpg) no-repeat left top; border:none; cursor:pointer; font-family:"Microsoft YaHei","Tahoma","Arial",'宋体'; color:#666; float:left; margin:8px 0 0 6px; display:inline;}
#search a.add{ background:url(../images/main/add.jpg) no-repeat -3px 7px #548fc9; padding:0 10px 0 26px; height:40px; line-height:40px; font-size:14px; font-weight:bold; color:#FFF; float:right}
#search a:hover.add{ text-decoration:underline; color:#d2e9ff;}
#main-tab{ border:1px solid #eaeaea; background:#FFF; font-size:12px;}
#main-tab th{ font-size:12px; background:url(../images/main/list_bg.jpg) repeat-x; height:32px; line-height:32px;}
#main-tab td{ font-size:12px; line-height:40px;}
#main-tab td a{ font-size:12px; color:#548fc9;}
#main-tab td a:hover{color:#565656; text-decoration:underline;}
.bordertop{ border-top:1px solid #ebebeb}
.borderright{ border-right:1px solid #ebebeb}
.borderbottom{ border-bottom:1px solid #ebebeb}
.borderleft{ border-left:1px solid #ebebeb}
.gray{ color:#dbdbdb;}
td.fenye{ padding:10px 0 0 0; text-align:right;}
.bggray{ background:#f9f9f9}
</style>
</head>
<body>
<!--main_top-->
<table width="99%" border="0" cellspacing="0" cellpadding="0" id="searchmain">
  <tr>
    <td width="99%" align="left" valign="top">您的位置：用户管理</td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="search">
  		<tr>
   		 <td width="90%" align="left" valign="middle">
	         <form method="get" action="list.php">
	         <span>用户名：</span>
	         <input type="text" name="username" class="text-word">
			 <span>i d：</span>
	         <input type="text" name="id" class="text-word">
	         <input type="submit" value="查询" class="text-but">
	         </form>
         </td>
  		  <td width="10%" align="center" valign="middle" style="text-align:right; width:150px;"><a href="add.php" target="mainFrame" onFocus="this.blur()" class="add">新增管理员</a></td>
  		</tr>
	</table>
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <form action="del.php" method="post">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="main-tab">
		<tr>
			<th align="center" valign="middle" class="borderright">id</th>
			<th align="center" valign="middle" class="borderright">用户名</th>
			<th align="center" valign="middle" class="borderright">性别</th>
			<th align="center" valign="middle" class="borderright">年龄</th>
			<th align="center" valign="middle" class="borderright">学历</th>
			<th align="center" valign="middle" class="borderright">头像</th>
			<th align="center" valign="middle" class="borderright">注册时间</th>
			<th align="center" valign="middle" class="borderright">注册ip</th>
			<th align="center" valign="middle" class="borderright">权限</th>
			<th align="center" valign="middle">操作</th>
		</tr>
		<?php
			//查询用户名
			if(!empty($_GET['username'])){
				$username = $_GET['username'];
				$wheres[] = "username like '%{$username}%'";
				$urls[] = "username={$username}" ;
			}
			//查询id
			if(!empty($_GET['id'])){
				$id = $_GET['id'];
				$wheres[] = "id like '%{$id}%'";
				$urls[] = "id={$id}";
			}
			//查询用户名与id
			if(is_array($wheres)){
				$where = 'where '.implode(' and ',$wheres);
				$url = '&'.implode('&',$urls);
			}
			
			//当前页
			$page = isset($_GET['page']) ? $_GET['page'] : 1;
			
			//每页显示几条
			$xpage = 5;
			
			//当前页-1 * 每页显示的条数
			$offset = ($page - 1) * $xpage;
			//分页
			$limit = "limit {$offset}, {$xpage}";
			
			//定义性别数组
			$sexarr = array(1 => '男', '女', '保密');
			//定义学历数组
			$xueliarr = array(1 => '小学', '初中', '高中', '大学', '烟酒生', '保密');
			//定义权限数组
			$qxarr = array(1 => '普通会员', '管理员');
			
			//准备SQL
			$sql = "select id,username,sex,age,xueli,pic,rtime,rip,qx from user {$where} {$limit} ";
			 // echo $sql;
			 // exit;
			
			//执行SQL
			$res = mysqli_query($link, $sql);
			
			//判断执行结果
			if($res && mysqli_num_rows($res) > 0){
				while($userinfo = mysqli_fetch_assoc($res)){
		?>
					<tr class="bggray" onMouseOut="this.style.backgroundColor='#f9f9f9'" onMouseOver="this.style.backgroundColor='#edf5ff'">
						<td align="center" valign="middle" class="borderright borderbottom"><input type="checkbox" name="sid[]" value="<?php echo $userinfo['id'];?>"><?php echo $userinfo['id'];?></td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo $userinfo['username'];?></td>
				<td align="center" valign="middle" class="borderright borderbottom"><?php echo $sexarr[$userinfo['sex']];?></td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo $userinfo['age'];?></td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo $xueliarr[$userinfo['xueli']];?></td>
						<td align="center" valign="middle" class="borderright borderbottom">
							<?php
								if($userinfo['pic'] == 'default.jpg'){
									echo '<img src="../../images/'.$userinfo['pic'].'">';
								}else{
									echo '<img src="../../images/s_'.$userinfo['pic'].'">';
								}
							?>
						</td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo date('Y-m-d H:i:s',$userinfo['rtime']);?></td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo long2ip($userinfo['rip']);?></td>
						<td align="center" valign="middle" class="borderright borderbottom"><?php echo $qxarr[$userinfo['qx']];?></td>
						<td align="center" valign="middle" class="borderbottom"><a href="edit.php?sid=<?echo $userinfo['id'];?>" target="mainFrame" onFocus="this.blur()" class="add">编辑</a><span class="gray">&nbsp;|&nbsp;</span><a href="del.php?sid=<?php echo $userinfo['id']?>" target="mainFrame" onFocus="this.blur()" class="add">删除</a></td>
					</tr>
		<?php
				
				}
			}
		?>
		
    </table>
	<input type="submit" value="批量删除">
	</form>
	</td>
    </tr>
	<?php
		//准备SQL
		$asql = "select count(*) from user {$where}";
		// echo $asql;
		// exit;
		//执行SQL
		$ares = mysqli_query($link, $asql);
		
		$a = mysqli_fetch_row($ares);
		
		//总页数 =  总条数/每页显示的条数
		$zpage = ceil($a[0]/$xpage);
		
		//上一页
		$prevpage = $page - 1;
		if($prevpage < 1){
			$prevpage = 1;
		}
		
		//下一页
		$nextpage = $page + 1;
		if($nextpage > $zpage){
			$nextpage = $zpage;
		}
	?>
  <tr>
    <td align="left" valign="top" class="fenye">共<?php echo $a[0];?>条数据 <?php echo $page;?>/<?php echo $zpage;?> 页&nbsp;&nbsp;
	<a href="list.php?page=1<?php echo $url;?>" target="mainFrame" onFocus="this.blur()">首页</a>&nbsp;&nbsp;
	<a href="list.php?page=<?php echo $prevpage.$url;?>" target="mainFrame" onFocus="this.blur()">上一页</a>&nbsp;&nbsp;
	<a href="list.php?page=<?php echo $nextpage.$url;?>" target="mainFrame" onFocus="this.blur()">下一页</a>&nbsp;&nbsp;
	<a href="list.php?page=<?php echo $zpage.$url;?>" target="mainFrame" onFocus="this.blur()">尾页</a></td>
  </tr>
</table>
</body>
</html>
<?php
//关闭数据库链接
mysqli_close($link);
?>